using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Net;
using System.Timers;
using System.IO;

namespace EquityXOI
{
    public class XOIVistor
    {
        private static System.Timers.Timer ReLoginTimer = null;
        private static AuthenticationConfigModel XOIAuthen = null;

        #region construct,singleton

        protected XOIVistor()
        {
            ReLoginTimer = new System.Timers.Timer();
            ReLoginTimer.Interval = 7 * 3600 * 1000;
            ReLoginTimer.Enabled = true;
            ReLoginTimer.Elapsed += new ElapsedEventHandler(ReLoginTimer_Elapsed);
            ReLoginTimer.Start();
            XOIAuthen = AuthenticationConfigManager.GetInstance().GetValidAuthencationByName("EquityXOI");
        }

        void ReLoginTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            m_LoginCookie = null;
        }

        private static volatile XOIVistor m_Instance;

        public static XOIVistor GetInstance()
        {
            if (m_Instance == null)
            {
                lock (typeof(XOIVistor))
                {
                    if (m_Instance == null)
                    {
                        m_Instance = new XOIVistor();
                    }
                }
            }
            return m_Instance;
        }

        #endregion


        private string LoginUrl
        {
            get 
            {
                if (XOIAuthen != null && !String.IsNullOrEmpty(XOIAuthen.Url))
                {
                    string rawUrl = XOIAuthen.Url;
                    if (rawUrl.IndexOf('?') != -1)
                    {
                        rawUrl += XOIAuthen.GetQueryString();
                    }
                    else
                    {
                        rawUrl += "?1=1" + XOIAuthen.GetQueryString();
                    }
                    return rawUrl;
                }
                return string.Empty;
            }
        }

        private string LoginTestUrl
        {
            get 
            {
                if (XOIAuthen != null && !String.IsNullOrEmpty(XOIAuthen.TestUrl))
                {
                    return XOIAuthen.TestUrl;
                }
                return string.Empty;
            }

        }

        private string LoginTag
        {
            get
            {
                if (XOIAuthen != null && !String.IsNullOrEmpty(XOIAuthen.SuccessFeature))
                {
                    return XOIAuthen.SuccessFeature;
                }
                return string.Empty;
            }
        }

        private string m_ContentUrl;

        public string ContentUrl
        {
            get { return m_ContentUrl; }
            set { m_ContentUrl = value; }
        }

        private volatile CookieContainer m_LoginCookie = null;

        private CookieContainer LoginCookie
        {
            get 
            {
                lock (typeof(CookieContainer))
                {
                    if (m_LoginCookie == null)
                    {
                        Login();
                    }
                }
                return m_LoginCookie;
            }
            set
            {
                m_LoginCookie = value;
            }
        }

        private bool Login()
        {
            CookieContainer ccLogin = null;
            bool res = false;
            try
            {
                string strLoginUrl = LoginUrl;
                HttpWebRequest webRequest;
                webRequest = (HttpWebRequest)WebRequest.Create(strLoginUrl);
                webRequest.CookieContainer = new CookieContainer();
                webRequest.AllowAutoRedirect = false;

                HttpWebResponse webResonse = (HttpWebResponse)webRequest.GetResponse();
                string cookie = webResonse.Headers.Get("Set-Cookie");

                ccLogin = new CookieContainer();
                ccLogin.SetCookies(webRequest.RequestUri, cookie);
                m_LoginCookie = ccLogin;

                res = true;
            }
            catch (System.Exception)
            {
                res = false;
            }
            return res;
        }

        public HttpWebResponse GetUrlResponse(string Url)
        {
            HttpWebRequest webReq;
            webReq = (HttpWebRequest)WebRequest.Create(Url);
            webReq.CookieContainer = LoginCookie;
            HttpWebResponse webResp2 = (HttpWebResponse)webReq.GetResponse();
            return webResp2;
        }

        public bool TestLogin()
        {
            HttpWebResponse webRes = GetUrlResponse(LoginTestUrl);
            Stream s = webRes.GetResponseStream();
            StreamReader sr = new StreamReader(s);
            if( sr.ReadToEnd() == LoginTag )
            {
                return true;
            }
            return false;
        }
    }
}
